﻿using CPPEI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XWeb.Core.Services
{
    public class CreateService
    {
        public ServiceResult CreateApp(string name, string icon)
        {
            using (XWebDbContext dbContext = new XWebDbContext())
            {
                EntityRepository<Entities.App> rep = new EntityRepository<Entities.App>(dbContext);

                Entities.App app = rep.Query().SingleOrDefault();
                if (app != null)
                {
                    return new ServiceResult(Error.COM_ObjExist);
                }
                app = new Entities.App();
                app.Id = Guid.NewGuid();
                app.Name = name;
                app.Icon = icon;
                app.CreatedTime = DateTime.Now;
                rep.Create(app);

                try
                {
                    dbContext.SaveChanges();
                    return new ServiceResult();
                }
                catch (Exception ex)
                {
                    return new ServiceResult(Error.COM_Exception, ex.Message);
                }
            }
        }

        public SingleResult<Entities.Media> CreateImage(Guid folderId,
            string name,
            string size,
            string relatviePath,
            string extension,
            string tag,
            string description)
        {
            using (XWebDbContext dbContext = new XWebDbContext())
            {
                if (folderId != Guid.Empty)
                {
                    EntityRepository<Entities.MediaFolder> folderRep = new EntityRepository<Entities.MediaFolder>(dbContext);
                    Entities.MediaFolder folder = folderRep.Get(p => p.Id == folderId);
                    if (folder == null)
                    {
                        return new SingleResult<Entities.Media>(Error.COM_ObjNotExist);
                    }
                }

                Entities.Media media = new Entities.Media();
                media.Id = Guid.NewGuid();
                media.Name = name;
                media.Extension = extension;
                media.FolderId = folderId;
                media.Description = description;
                media.Tags = tag;
                media.Size = size;
                media.Type = Def.MediaType.Image;
                media.CreatedTime = DateTime.Now;

                EntityRepository<Entities.Media> rep = new EntityRepository<Entities.Media>(dbContext);
                rep.Create(media);

                try
                {
                    dbContext.SaveChanges();

                    return new SingleResult<Entities.Media>(media);
                }
                catch (Exception ex)
                {
                    return new SingleResult<Entities.Media>(Error.COM_Exception, ex.Message);
                }
            }
        }


        public SingleResult<Entities.MediaFolder> CreateFolder(Guid parentId,
            string name)
        {
            using (XWebDbContext dbContext = new XWebDbContext())
            {
                EntityRepository<Entities.MediaFolder> folderRep = new EntityRepository<Entities.MediaFolder>(dbContext);
                if (parentId != Guid.Empty)
                {
                    Entities.MediaFolder parentFolder = folderRep.Get(p => p.Id == parentId);
                    if (parentFolder == null)
                    {
                        return new SingleResult<Entities.MediaFolder>(Error.COM_ObjNotExist);
                    }
                }

                Entities.MediaFolder folder = new Entities.MediaFolder();
                folder.CreatedTime = DateTime.Now;
                folder.Index = 0;
                folder.Id = Guid.NewGuid();
                folder.ParentId = parentId;
                folderRep.Create(folder);


                try
                {
                    dbContext.SaveChanges();

                    return new SingleResult<Entities.MediaFolder>(folder);
                }
                catch (Exception ex)
                {
                    return new SingleResult<Entities.MediaFolder>(Error.COM_Exception, ex.Message);
                }
            }
        }


        public SingleResult<Entities.Logic> CreateLogic(string name, string assembly)
        {
            using (XWebDbContext dbContext = new XWebDbContext())
            {
                EntityRepository<Entities.Logic> logicRep = new EntityRepository<Entities.Logic>(dbContext);
                Entities.Logic logic = logicRep.Get(p => p.Name == name);
                if(logic != null)
                {
                    return new SingleResult<Entities.Logic>(Error.COM_ObjExist);
                //    dbContext.Database.ExecuteSqlCommand("delete from xweb_logic where id={0}", logic.Id);
                //    dbContext.Database.ExecuteSqlCommand("delete from xweb_logicaction where logicid={0}", logic.Id);
                }

                DateTime now = DateTime.Now;
                logic = new Entities.Logic();
                logic.Id = Guid.NewGuid();
                logic.Name = name;
                logic.Assembly = assembly;

                logicRep.Create(logic);

                try
                {
                    dbContext.SaveChanges();
                    return new SingleResult<Entities.Logic>(logic);
                }
                catch(Exception ex)
                {
                    return new SingleResult<Entities.Logic>(Error.COM_Exception, ex.Message);
                }

            }
        }
    }
}
